<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item :label="$t('radio.taskName')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'name', validatorRules.name]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>

        <a-form-item :label=" $t('base.startTime') " :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date :placeholder="$t('base.pleaseChoose')" v-decorator="[ 'startTime', validatorRules.startTime]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>

        <a-form-item :label=" $t('base.endTime') " :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date :placeholder="$t('base.pleaseChoose')" v-decorator="[ 'endTime', validatorRules.endTime]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>

        <a-form-item :label=" $t('radio.playTime') " :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date :placeholder="$t('base.pleaseChoose')" showTime="true" dateFormat="HH:mm" v-decorator="[ 'division', validatorRules.division]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>

        <a-form-item :label=" $t('radio.volume')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'playVol', validatorRules.playVol]" :placeholder="$t('base.pleaseEnter')" style="width: 100%"/>
        </a-form-item>

        <a-form-item :label=" $t('radio.playDurationMinute')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'playTime', validatorRules.playTime]" :placeholder="$t('base.pleaseEnter')" style="width: 100%"/>
        </a-form-item>

        <a-form-item :label=" $t('radio.radioManufacturer')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag  v-decorator="['companyCode', validatorRules.companyCode]" :triggerChange="true" :placeholder="$t('base.pleaseChoose')"
                              dictCode="glr_yzgs"/>
        </a-form-item>

        <a-form-item :label=" $t('radio.tips') " :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="model.status==0||model.status==1">
          <span style="color: #0000FF"><a-icon type="exclamation-circle" />
            {{$t('radio.currentTaskStatus')}}
            <span v-show="model.status==0">[ {{$t('radio.startUsed')}} ],</span>
            <span v-show="model.status==1">[ {{$t('radio.discontinueUsed')}} ],</span>
            {{$t('radio.taskStatusWillBeUpdated')}}
            <span v-show="model.status==0">[ {{$t('radio.startUsed')}} ],</span>
            <span v-show="model.status==1">[ {{$t('radio.discontinueUsed')}} ],</span>
          </span>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  

  export default {
    name: "GlrRadioTaskModal",
    components: { 
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          name: {rules: [
            {required: true, message: this.$t('base.pleaseEnter')+'!'},
          ]},
          startTime: {rules: [
            {required: true, message: this.$t('base.pleaseChoose')+'!'},
          ]},
          endTime: {rules: [
            {required: true, message: this.$t('base.pleaseChoose')+'!'},
          ]},
          division: {rules: [
            {required: true, message: this.$t('base.pleaseEnter')+'!'},
          ]},
          playVol: {rules: [
            {required: true, message: this.$t('base.pleaseEnter')+'!'},
          ]},
          status: {rules: [
          ]},
          taskId: {rules: [
          ]},
          playTime: {rules: [
              {required: true, message: this.$t('base.pleaseEnter')+'!'},{ validator: this.handlePlayTime }
          ]},
          companyCode: {rules: [
              {required: true, message: this.$t('base.pleaseChoose')+'!'},
          ]},
        },
        url: {
          add: "/glr/glrRadioTask/add",
          edit: "/glr/glrRadioTask/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'name','startTime','endTime','division','playVol','status','startStatus','taskId','playTime','companyCode','sysCompanyCode'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handlePlayTime(rule, value, callback){
        if (value>600){
          callback( this.$t('radio.noMoreThan600Minutes') );
        }
        if (value<1){
          callback(this.$t('radio.notLessThan1Minute') );
        }
        callback();
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                //that.$message.success(res.message);
                that.$message.success(that.$t('base.operationSuccessful'));
                that.$emit('ok');
              }else{
                //that.$message.warning(res.message);
                that.$message.warning(that.$t('base.operationFailed'));
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'name','startTime','endTime','division','playVol','status','startStatus','taskId','playTime','companyCode','sysCompanyCode'))
      },

      
    }
  }
</script>